package com.lb.shopguide.ui.view.chart;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.blankj.utilcode.util.LogUtils;
import com.lb.shopguide.entity.local.InOutData;
import com.lb.shopguide.util.lb.BizUtil;

import java.util.ArrayList;

/**
 * Created by kang on 2018/2/27.
 */

public class LbBarView extends View {
    private int mViewWidth; //view的宽
    private int mViewHeight;    //view的高

    private Paint mBaseLinePaint;
    private Paint mTextPaint;
    private Paint mProTextPaint;
    private Paint mInComePaint;
    private Paint mOutComePaint;

    private int barWidth = 50;
    private int barHeight = 368;
    private int allBarHeight = 408;
    private int amount = 4;
    private int amountWidth;
    private float fraction;

    private float density;
    private float scaleDensity;

    private ArrayList<InOutData> mList;
    private float maxData;
    protected ValueAnimator mAnimator;//动画对象


    public LbBarView(Context context) {
        super(context);
    }

    public LbBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initParams();
        initPaint();
    }

    public LbBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initParams() {
        density = getResources().getDisplayMetrics().density / 2;
        scaleDensity = getResources().getDisplayMetrics().density / 2;

        barWidth = (int) (barWidth * density);
        barHeight = (int) (barHeight * density);
        allBarHeight = (int) (allBarHeight * density);
    }

    private void initPaint() {
        mBaseLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBaseLinePaint.setAntiAlias(true);

        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setAntiAlias(true);

        mProTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mProTextPaint.setAntiAlias(true);

        mInComePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mInComePaint.setAntiAlias(true);
        mInComePaint.setTextSize(18 * scaleDensity);
        mInComePaint.setColor(Color.rgb(255, 99, 99));

        mOutComePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mOutComePaint.setAntiAlias(true);
        mOutComePaint.setTextSize(18 * scaleDensity);
        mOutComePaint.setColor(Color.rgb(148, 205, 255));
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mViewWidth = getMeasuredWidth();
        mViewHeight = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mList == null) {
            return;
        }
        if (maxData == 0.0f) {
            maxData = 1;
        }
        drawBar(canvas);
    }

    public void setData(ArrayList<InOutData> list, float maxData) {
        mList = list;
        amount = mList.size();
        this.maxData = maxData;
        startAnim();
    }

    private void drawBar(Canvas canvas) {
        amountWidth = mViewWidth / amount;
        drawBaseLine(canvas);
        for (int i = 0; i < amount; i++) {
            //绘制日期
            drawTwoBar(canvas, mList.get(i), i);
        }
    }

    private void drawBaseLine(Canvas canvas) {
        mBaseLinePaint.setColor(Color.rgb(222, 222, 222));
        canvas.drawLine(0, allBarHeight, mViewWidth, allBarHeight, mBaseLinePaint);
    }

    private void drawTwoBar(Canvas canvas, InOutData data, int pos) {
        String text = data.getDate();
        float textWidth = mTextPaint.measureText(text);
        drawDate(canvas, mViewWidth / amount / 2 - textWidth / 2 + amountWidth * pos, allBarHeight + 20 * density, text);

        float incomeWidth = mTextPaint.measureText("" + data.getInData());
        float inPro = 1 - data.getInData() / maxData;
        float inBarHeight = Math.abs(inPro * barHeight + 40 * density - allBarHeight);
        canvas.drawRect(30 * density + amountWidth * pos, allBarHeight - inBarHeight * fraction, 30 * density + barWidth + amountWidth * pos, allBarHeight, mInComePaint);
        drawProText(canvas, 55 * density + amountWidth * pos - incomeWidth / 2, inPro * barHeight + 40 * density, "" + data.getInData(), mInComePaint);

        float outcomeWidth = mTextPaint.measureText("" + data.getOutData());
        float outPro = 1 - data.getOutData() / maxData;
        float outBarHeight = Math.abs(outPro * barHeight + 40 * density - allBarHeight);
        canvas.drawRect(100 * density + amountWidth * pos, allBarHeight - outBarHeight * fraction, 100 * density + barWidth + amountWidth * pos, allBarHeight, mOutComePaint);
        drawProText(canvas, 125 * density + amountWidth * pos - outcomeWidth / 2, outPro * barHeight + 40 * density, "" + data.getOutData(), mOutComePaint);
        LogUtils.d("=====drawTwoBar=====" + fraction);
    }

    private void drawProText(Canvas canvas, float baseX, float baseY, String text, Paint paint) {
        //画文字
        text = BizUtil.convertBigNumber(text, 2);
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
        float offY = fontTotalHeight - fontMetrics.bottom;
        float newY = baseY - offY;

        canvas.drawText(text, baseX, newY, paint);
    }

    private void drawDate(Canvas canvas, float baseX, float baseY, String text) {
        //画文字
        mTextPaint.setColor(Color.rgb(46, 46, 46));
        mTextPaint.setTextSize(20 * scaleDensity);
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
        float offY = fontTotalHeight - fontMetrics.bottom;
        float newY = baseY + offY;

        canvas.drawText(text, baseX, newY, mTextPaint);
    }

    protected void startAnim() {
        //动画正在运行的话，先stop吧。万一有人要使用新动画呢，（正经用户不会这么用。）
        stopAnim();

        mAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
        mAnimator.setInterpolator(new DecelerateInterpolator());
        mAnimator.setDuration(1000);
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                fraction = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        mAnimator.start();
    }

    /**
     * 停止动画
     */
    public void stopAnim() {
        if (null != mAnimator) {
            mAnimator.cancel();
        }
    }
}
